package com.dabai.dbutils.utils;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.IntDef;
import androidx.core.app.ActivityCompat;

/**
 * 手机网络状态工具类
 */
public class NetUtils {

    /**
     * 打开网络设置界面
     *
     * @param mContext m上下文
     */
    public static void openNetworkSetting(Context mContext) {
        mContext.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }

    /**
     * 获取网络类型
     * WiFi : 1   流量 : 2  无网络 : 0
     * @param context 上下文
     * @return int
     */
    public static int getNetType(Context context){
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isWifiConn = networkInfo.isConnected();
        if (isWifiConn) {
            return 1;
        }
        networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        boolean isMobileConn = networkInfo.isConnected();
        if (isMobileConn) {
            return 2;
        }

        return 0;
    }


    /**
     * 枚举网络状态 NET_NO：没有网络 NET_2G:2g网络 NET_3G：3g网络 NET_4G：4g网络 NET_WIFI：wifi
     * NET_UNKNOWN：未知网络
     */
    public class NetState {
        public static final int NET_NO_CONNECT = 0;
        public static final int NET_WIFI = 1;
        public static final int NET_2G = 2;
        public static final int NET_3G = 3;
        public static final int NET_4G = 4;
        public static final int NET_5G = 5;
        public static final int NET_UNKNOWN = -1;
        /**
         * 没有READ_PHONE_STATE 权限,不过是可以知道当前是 移动网络,不是wifi
         */
        public static final int NOT_PERMISSION_READ_PHONE_STATE_ONLY_GPRS = -2;
        public static final int NOT_PERMISSION_ACCESS_NETWORK_STATE = -3;
    }

    @IntDef({NetState.NET_NO_CONNECT, NetState.NET_2G, NetState.NET_3G, NetState.NET_4G, NetState.NET_5G
            , NetState.NET_UNKNOWN, NetState.NET_WIFI
            , NetState.NOT_PERMISSION_ACCESS_NETWORK_STATE, NetState.NOT_PERMISSION_READ_PHONE_STATE_ONLY_GPRS})
    public @interface NetStateInt {

    }

    /**
     * 判断当前网络连接
     * android N 以上用 NetworkCapabilities 和 TelephonyManager.getDataNetworkType()
     *
     *  {ACCESS_NETWORK_STATE (必须) , READ_PHONE_STATE (可无,有的话可以判断是 3g/4g)}
     * @param context 上下文
     * @return 状态码 @NetStateInt
     */
    public static @NetStateInt
    int getNetType(Context context, Network network
            , NetworkCapabilities netCapabilities) {
        int stateCode = NetState.NET_UNKNOWN;

        final String tag_ = "getNetType";
//            ACCESS_NETWORK_STATE
        if (!DabaiUtils.isPermissionChecked(context
                , Manifest.permission.ACCESS_NETWORK_STATE)) {
            return NetState.NOT_PERMISSION_ACCESS_NETWORK_STATE;
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {

            Network ni = network;
            if (ni == null) {

                return NetState.NET_NO_CONNECT;
            }
            NetworkCapabilities capabilities = netCapabilities;

            if (capabilities == null) {

                return NetState.NET_NO_CONNECT;
            }

            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                stateCode = NetState.NET_WIFI;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                if (!DabaiUtils.isPermissionChecked(context
                        , Manifest.permission.READ_PHONE_STATE)) {
                    return NetState.NOT_PERMISSION_READ_PHONE_STATE_ONLY_GPRS;
                }
                TelephonyManager tm = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                // READ_PHONE_STATE

                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return NetState.NOT_PERMISSION_READ_PHONE_STATE_ONLY_GPRS;
                }
                int type = tm.getDataNetworkType();
                if (type == TelephonyManager.NETWORK_TYPE_UNKNOWN){
                    return NetState.NET_UNKNOWN;
                }
//                boolean hasCarr = tm.hasCarrierPrivileges();

//                L.d(tag_,"hasCarrierPrivileges  =="+hasCarr);
                switch (type) {

                    case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
                    case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
                    case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        stateCode = NetState.NET_2G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        stateCode = NetState.NET_3G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        stateCode = NetState.NET_4G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_NR:
                        stateCode = NetState.NET_5G;
                        break;
                    default:

                        stateCode = NetState.NET_UNKNOWN;
                        break;
                }
            }else {

                stateCode = NetState.NET_NO_CONNECT;
            }


        } else {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null && ni.isConnectedOrConnecting()) {
                switch (ni.getType()) {
                    case ConnectivityManager.TYPE_WIFI:
                        stateCode = NetState.NET_WIFI;
                        break;
                    case ConnectivityManager.TYPE_MOBILE:
                        switch (ni.getSubtype()) {
                            case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
                            case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
                            case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
                            case TelephonyManager.NETWORK_TYPE_1xRTT:
                            case TelephonyManager.NETWORK_TYPE_IDEN:
                                stateCode = NetState.NET_2G;
                                break;
                            case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                            case TelephonyManager.NETWORK_TYPE_UMTS:
                            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                            case TelephonyManager.NETWORK_TYPE_HSDPA:
                            case TelephonyManager.NETWORK_TYPE_HSUPA:
                            case TelephonyManager.NETWORK_TYPE_HSPA:
                            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                            case TelephonyManager.NETWORK_TYPE_EHRPD:
                            case TelephonyManager.NETWORK_TYPE_HSPAP:
                                stateCode = NetState.NET_3G;
                                break;
                            case TelephonyManager.NETWORK_TYPE_LTE:
                                stateCode = NetState.NET_4G;
                                break;
                            case TelephonyManager.NETWORK_TYPE_NR:
                                stateCode = NetState.NET_5G;
                                break;
                            default:
                                stateCode = NetState.NET_UNKNOWN;
                        }
                        break;
                    default:
                        stateCode = NetState.NET_UNKNOWN;
                }

            }
        }
        return stateCode;
    }


}
